//
//  LFBRightTableView.m
//  LFBLoveFreshBee
//
//  Created by 张睿 on 16/8/20.
//  Copyright © 2016年 丁鼎. All rights reserved.
//

#import "LFBRightTableView.h"

@interface LFBRightTableView ()

//@property (nonatomic, strong )UITableView * rightTableView;

@property (nonatomic, strong)NSArray * productsArray;

@property (nonatomic, strong)NSArray * sectionArr;
//是否是向上滚动
@property(nonatomic, assign)BOOL isScrollUp;
//滚动即将结束时scrollView的偏移量
@property(nonatomic, assign)CGFloat lastOffsetY;

@end

@implementation LFBRightTableView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isScrollUp = false;
    _lastOffsetY = 0;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToSelectedIndexPath:) name:@"LeftNotificationRight" object:nil];
    
    [self setupUI];
}

- (void)setupUI{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Right"];
    //
    //    [self.view addSubview:self.rightTableView];
}


#pragma mark - UITableViewDataSource -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return [NSString stringWithFormat:@"第%ld类",(long)section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Right"];
    cell.backgroundColor = [UIColor redColor];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld号商品",(long)indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    _isScrollUp = _lastOffsetY < scrollView.contentOffset.y;
    _lastOffsetY = scrollView.contentOffset.y;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(nonnull UIView *)view forSection:(NSInteger)section{
    NSString * sectionString = [NSString stringWithFormat:@"%ld",section];
    
    NSDictionary * dict = @{
                            @"number":sectionString
                            };
    if (_isScrollUp == NO) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"willDisplay" object:nil userInfo:dict];
    }
    
}

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(nonnull UIView *)view forSection:(NSInteger)section{
    NSString * sectionString = [NSString stringWithFormat:@"%ld",section];
    
    NSDictionary * dict = @{
                            @"number":sectionString
                            };
    if (_isScrollUp) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didEndDisplaying" object:nil userInfo:dict];
    }
    
}

#pragma mark - 通知方法 -
- (void)scrollToSelectedIndexPath:(NSNotification *)dict{
    
    NSString * rowStr = dict.userInfo[@"row"];
    int row = [rowStr intValue];
    
    //    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:row] animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LeftNotificationRight" object:nil];
}

//- (UITableView *)rightTableView{
//    if (_rightTableView == nil) {
//         _rightTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
//        _rightTableView.delegate = self;
//        _rightTableView.dataSource = self;
////        [_rightTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Right"];
//
//        [self.view addSubview:_rightTableView];
//    }
//
//    return _rightTableView;
//}
 

@end
